// packageE/community_solitaire/components/yz_cartList/yz_cartList.js
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
const cartPost = require('../cart');
const app = getApp();
Component({
  behaviors: [skinBehavior, cartPost],
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      value: false,
      type: Boolean,
    },
    cardata: {
      value: [],
      type: Array,
    },
    showDestory: {
      type: Boolean,
      value: true
    },
    store_id: {
      type: null
    }
  },
  options: {
    multipleSlots: true,
  },
  lifetimes: {
    attached() {
      wx.getStorage({
        key: 'langIndex',
        success: (res) => {
          this.setData({
            'language': res.data.en
          });
        }
      });
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    timer: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clearCart(e) {
      let {key} = e.currentTarget.dataset;
      var destroyCarts = [];
      if (this.data.cardata.length == 0) {
        return app.tips('请选择购物车商品')
      }
      this.data.cardata.forEach(item => {
        if(key == 'fail'){
          if(item.is_failure == 1){
            destroyCarts.push(item.id);
          }
        }else{
          destroyCarts.push(item.id);
        }
      });
      wx.showLoading({
        title: '加载中',
      })
      app._postNetWork({
        showToastIn: false,
        url: app.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.destroy"),
        data: {
          store_id: this.data.store_id,
          ids: destroyCarts.toString()
        },
        success: async (resdata) => {
          wx.hideLoading()
          var res = resdata.data;
          if (res.result == 1) {
            this.triggerEvent('updateCart')
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: () => {
          wx.hideLoading()
        }
      });
    },
    popupcartListbtn() {
      this.triggerEvent("popupcartListbtn", {
        clicktype: 0
      });
    },
    closedCarList() {
      this.triggerEvent('closedCarList')
    },
    reduceCartNum(evt) {
      let {
        item
      } = evt.currentTarget.dataset;
      if (item.option_id) {
        // 有规格
        this.updateCartRequest(item.id, '-1', this.data.store_id, item.option_id)
      } else {
        // 无规格
        this.updateCartRequest(item.id, '-1', this.data.store_id)
      }
      console.log(item)
    },
    addCartNum(evt) {
      let {
        item
      } = evt.currentTarget.dataset;
      if (item.option_id) {
        // 有规格
        this.updateCartRequest(item.id, '1', this.data.store_id, item.option_id)
      } else {
        // 无规格
        this.updateCartRequest(item.id, '1', this.data.store_id)
      }
    },
    // addCartNum(evt) {
    //   let item = evt.currentTarget.dataset.item;
    //   item.name = 'plus';
    //   this.data.timer = setTimeout(() => {
    //     this.triggerEvent("addCart", item);
    //   }, 200);
    // },
    focusInp(e) {
      console.log(e)
      this.setData({
        goodsCartsOld: e.detail.value
      })
    },
    updateCartNum(evt) {
      let item = evt.target.dataset.item;
      item.total = evt.detail.value;
      if (evt.detail.value == "" || evt.detail.value == 0) {
        item.total = 0;
      }
      item.name = 'blur';
      if (item.total < this.data.goodsCartsOld) {
        let lowerNum = this.data.goodsCartsOld - Number(item.total);
        this.updateCartRequest(item.id, `-${lowerNum}`, this.data.store_id)
      } else if (item.total > this.data.goodsCartsOld) {
        let lowerNum = Number(item.total) - this.data.goodsCartsOld;
        this.updateCartRequest(item.id, lowerNum, this.data.store_id)
      }
    },
  },
});